﻿"use strict";

window.ArthurGraus = window.ArthurGraus || {};

ArthurGraus.Analytics = function (appCode) {
    var self = this;
    self.appCode = appCode;
    self.appWebUrl = "";
    self.hostWebUrl = "";
    self.context = null;
    self.appWebContext = null;
    self.hostWebContext = null;
    self.hostWeb = null;
    self.ads = null;
    self.index = 0;
    self.language = "en-US";
    self.email = "";

    self.initialize = function () {
        self.appWebUrl = decodeURIComponent(getQuerystring("SPAppWebUrl"));
        self.hostWebUrl = decodeURIComponent(getQuerystring("SPHostUrl"));
        self.language = decodeURIComponent(getQuerystring("SPLanguage"));
        self.setAnalytics();
    };

    self.setAnalytics = function () {
        var url = "//apps.arthurgraus.nl/Services/AdRotatorService.svc/Analytics/{APPCODE}/{LANGUAGE}?u={USERNAME}&h={HOSTWEBURL}&p={APPPAGE}";

        url = url.replace("{APPCODE}", self.appCode);
        url = url.replace("{LANGUAGE}", self.language)
        url = url.replace("{USERNAME}", encodeURIComponent(self.email));
        url = url.replace("{HOSTWEBURL}", encodeURIComponent(self.hostWebUrl));
        url = url.replace("{APPPAGE}", encodeURIComponent(self.appWebUrl));

        $.ajax({
            type: "GET",
            url: url,
            dataType: "jsonp",
            crossDomain: true,
            success: function (results) {
            },
            error: function (result) {
            }
        });
    };

    self.error = function (sender, args) {
    };

}

function base64Encode(str) {
    var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var out = "", i = 0, len = str.length, c1, c2, c3;
    while (i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if (i == len) {
            out += CHARS.charAt(c1 >> 2);
            out += CHARS.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if (i == len) {
            out += CHARS.charAt(c1 >> 2);
            out += CHARS.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
            out += CHARS.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += CHARS.charAt(c1 >> 2);
        out += CHARS.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
        out += CHARS.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
        out += CHARS.charAt(c3 & 0x3F);
    }
    return out;
}
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
function htmlEscape(str) {
    return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/ /g, '&nbsp;')
            .replace(/\n/g, '<br />');
}


